<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;

class Music extends Controller
{
	  public function lst()
	  {
		  $getMusicList = Db::name('music')->select();
		  $this->assign('title','音乐列表');
		  $this->assign('getMusicList',$getMusicList);
		  return view();
	  }
	  
	  /*
		 添加音乐
	  */
	 public function add()
	 {
		 if(request()->isPost()){
			$data = input('post.');
			try{
				$file = request()->file('music_file');
				if($file){
					$info = $file->validate(['ext'=>'mp3'])->move( '../public/uploads/music');
					if($info){
						$data['music_file'] = str_replace("\\","/",$info->getSaveName());
					}else{
						$res = $file->getError();
						return $this->error($res);
					}
				}
			}catch (\Exception $e) {
                $this->error($e->getMessage());
            }
			
			$result = Db::name('music')->insert($data);
			if($result){
				$this->success('音乐上传成功');
			}else{
				$this->error('音乐上传失败');
			}
		 }
		return view();
	 }
}
